#import "tool_movement.h"

bool tmvComputeParabola(TMVParabolaData *Data)
{
	float _X1;
	float _Y1;
	float _X2;

	if ((NULL == Data) || (0 >= Data->start_speed.y))
	{
		return false;
	}

	/*计算时间*/
	Data->half_time = Data->start_speed.y / G;
	Data->time = Data->half_time * 2;
	/*计算高度*/
	Data->height = 0.5 * G * Data->half_time * Data->half_time;
	/*计算跨距*/
	Data->span = Data->start_speed.x * Data->time;
	/*计算参数*/
	_X1 = Data->span / 2;
	_Y1 = Data->height;
	_X2 = Data->span;
	Data->b = _Y1 / (-1 * (_X1 * _X1 / _X2) + _X1);
	Data->a = -1 * Data->b / _X2;
	return true;
}

void tmvComputeParabolaPoint(TMVParabolaData *Data, float Time, CGPoint *Buf)
{
	float _TempTime;

	Buf->x = Time * Data->start_speed.x;
	if (Time < Data->half_time)
	{
		Buf->y = Data->start_speed.y * Time - 0.5 * G * Time * Time;
	}
	else if (Time > Data->half_time)
	{
		_TempTime = Time - Data->half_time;
		Buf->y = Data->height - (0.5 * G * _TempTime * _TempTime);
	}
	else
	{
		Buf->y = Data->height;
	}
}

void tmvComputeParabolaPointByX(TMVParabolaData *Data, float X, CGPoint *Buf)
{
	Buf->x = X;
	Buf->y = Data->a * X * X + Data->b * X;
}

void tmvComputeParabolaPointByY(TMVParabolaData *Data, float Y, CGPoint *Buf1, CGPoint *Buf2)
{
	float _Temp;

	_Temp = sqrt(Data->b * Data->b - 4 * Data->a * -1 * Y);
	Buf1->x = (-1 * Data->b + _Temp) / (2 * Data->a);
	Buf1->y = Y;
	Buf2->x = (-1 * Data->b - _Temp) / (2 * Data->a);
	Buf2->y = Y;
}

float tmvComputeParabolatTangentSlope(TMVParabolaData *Data, CGPoint *Point)
{
	/*切线的直线方程:y=ax+b,切线方程:y=(2aX+b)x-aX^2*/
	float _a;
	float _b;
	float _x;
	float _y;
	float _BaseAngle;

	_a = 2 * Data->a * Point->x + Data->b;
	_b = -1 * Data->a * Point->x * Point->x;
	_x = fabs(-1 * _b / _a);
	_y = fabs(_b);
	_BaseAngle = CC_RADIANS_TO_DEGREES(atan(_x / _y));
	if (Data->start_speed.x > 0.0f)
	{
		if (Point->x < Data->span / 2)
		{
			return _BaseAngle;
		}
		else if (Point->x > Data->span / 2)
		{
			return 180.0f - _BaseAngle;
		}
		else
		{
			return 90.0f;
		}
	}
	else if (Data->start_speed.x < 0.0f)
	{
		if (Point->x > Data->span / 2)
		{
			return -1 * _BaseAngle;
		}
		else if (Point->x < Data->span / 2)
		{
			return -180.0f + _BaseAngle;
		}
		else
		{
			return -90.0f;
		}
	}
	else
	{
		return 0.0f;
	}
}

bool tmvComputeLinear(TMVLinearData *Data)
{
	/*计算直线参数*/
	Data->a = (Data->start_pos.y - Data->end_pos.y) / (Data->start_pos.x - Data->end_pos.x);
	Data->b = Data->start_pos.y - Data->a * Data->start_pos.x;
	/*计算角度*/
	tmvComputeLinearAngle(Data);
	/*计算速度*/
	tmvComputeLinearSplitSpeed(Data);
	/*计算距离*/
	Data->distance = ptPointDistance_CGPoint(Data->start_pos, Data->end_pos);
	return true;
}

void tmvComputeLinearPoint(TMVLinearData *Data, float Time, CGPoint *Buf)
{
	Buf->x = Data->start_pos.x + Time * Data->speed.x;
	Buf->y = Data->start_pos.y + Time * Data->speed.y;
	if (!Data->flag_infinity)
	{
		if (ptPointDistance_CGPoint(*Buf, Data->end_pos) >= Data->distance)
		{
			Buf->x = Data->end_pos.x;
			Buf->y = Data->end_pos.y;
		}
	}
}

void tmvComputeLinearAngle(TMVLinearData *Data)
{
	float _x, _y;
	float _Radius;

	_x = abs(Data->end_pos.x - Data->start_pos.x);
	_y = abs(Data->end_pos.y - Data->start_pos.y);
	_Radius = atan(_x / _y);
	if (Data->end_pos.x < Data->start_pos.x)
	{
		Data->angle = -1 * CC_RADIANS_TO_DEGREES(_Radius);
	}
	else
	{
		Data->angle = CC_RADIANS_TO_DEGREES(_Radius);
	}
}

void tmvComputeLinearSplitSpeed(TMVLinearData *Data)
{
	Data->speed.x = (Data->end_pos.x > Data->start_pos.x ? 1 : -1) * abs(sin(CC_DEGREES_TO_RADIANS(Data->angle)) * Data->base_speed);
	Data->speed.y = (Data->end_pos.y > Data->start_pos.y ? 1 : -1) * abs(cos(CC_DEGREES_TO_RADIANS(Data->angle)) * Data->base_speed);
}